// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import Client from '@api/sparepartshospital/api/SpareStockRecordApi'

import ComClass from '@api/ComQuery'
import ListClass from '@api/sparepartshospital/model/SpareStockRecordListQueryReq'
import DelClass from '@api/sparepartshospital/model/SpareStockRecordReq'

const CLIENT = CORE.grpcCLIENT(Client)
const COM = [ComClass, 'comQuery']

// ============================== 导出远程接口 ============================== //

export default {
  // —————————————————————————————— 列表 —————————————————————————————— //

  validParam(param, { MAP: { TAB } }) {
    return param.tab === TAB
  },
  async getList(param, TIP = '查询列表') {
    const warehouseId = param.warehouseId ? [param.warehouseId] : []
    const DTO = CORE.grpcDTO(ListClass, { ...param, warehouseId }, COM)
    const resp = await CORE.grpc(TIP, CLIENT, 'querySpareStockRecordSpareList', DTO)
    return [resp.comQuery.totalCount, resp.content || []]
  },
  transList(arr) {
    arr.forEach((item) => {
      if (CORE.isEmpty(item.content)) item.content = [{}]
      item.id = `${item.id}-${item.content[0].id || ''}`
    })
    return arr
  },

  // —————————————————————————————— 删 —————————————————————————————— //

  transDel(one) {
    return one.content[0]
  },
  async del({ id }, TIP = '删除') {
    const DTO = CORE.grpcDTO(DelClass, { ids: [id] })
    await CORE.grpc(TIP, CLIENT, 'deleteSpareStockRecordSpare', DTO)
    return CORE.grpcOK(TIP)
  },
}
